<?php
class ControllerAuth extends Zend_Controller_Plugin_Abstract {
	
	/**
	 *
	 * @param unknown_type $haystack        	
	 * @param unknown_type $needle        	
	 */
	private function _startsWith($haystack, $needle) {
		return strpos ( $haystack, $needle, 0 ) === 0;
	}
	public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
		$configs = array ();
		$confFile = APPLICATION_PATH . "/configs/auth_controllers.ini";
		if (file_exists ( $confFile )) {
			$ini = new Zend_Config_Ini ( $confFile );
			$configs = $ini->toArray ();
		}
		
		// Skip actions
		$currentModule = $request->getModuleName ();
		$currentController = $request->getControllerName ();
		$currentAction = $request->getActionName ();
		
		foreach ( $configs as $confMod => $val1 ) {
			if (is_array ( $val1 )) {
				foreach ( $val1 as $confCont => $val2 ) {
					if (is_array ( $val2 )) {
						foreach ( $val2 as $confAct => $val3 ) {
							if ($currentModule == $confMod && $currentController == $confCont && ($currentAction == $confAct || $confAct == "ALL")) {
								return;
							}
						}
					}
				}
			}
		}
		
		if ($this->_startsWith ( $request->getControllerName (), 'crossover' )) {
			
			$auth = Zend_Auth::getInstance ();
			$auth->setStorage ( new Zend_Auth_Storage_Session ( 'CROSSOVER_AUTH' ) );
			// var_dump(Zend_Auth::getInstance()->getStorage()->read());die;
			if (! $auth->hasIdentity ()) {
				$crossoverNoAuthDispatchFor = $configs ['crossover_noauth_dispatch_for'];
				list ( $mod, $con, $act ) = explode ( ".", $crossoverNoAuthDispatchFor );
				
				if (Zend_Session::isStarted ()) {
					$sess = new Zend_Session_Namespace ( 'Kzend_Auth' );
					$sess->currentModule = $request->getModuleName ();
					$sess->currentController = $request->getControllerName ();
					$sess->currentAction = $request->getActionName ();
					$sess->currentRequestUri = $_SERVER ['REQUEST_URI'];
				}
				
				$frontController = Zend_Controller_Front::getInstance ();
				$response = $frontController->getResponse ();
				if (! empty ( $act )) {
					$response->setRedirect ( $frontController->getBaseUrl () . '/' . $con . '/' . $act );
				} else {
					$response->setRedirect ( $frontController->getBaseUrl () . '/' . $con );
				}
				$response->sendHeaders ();
				exit ();
			}
		} elseif ($this->_startsWith ( $request->getControllerName (), 'company' )) {
			$auth = Zend_Auth::getInstance ();
			$auth->setStorage ( new Zend_Auth_Storage_Session ( 'COMPANY_AUTH' ) );
			// var_dump(Zend_Auth::getInstance()->getStorage()->read());die;
			if (! $auth->hasIdentity ()) {
				$companyNoAuthDispatchFor = $configs ['company_noauth_dispatch_for'];
				list ( $mod, $con, $act ) = explode ( ".", $companyNoAuthDispatchFor );
				
				if (Zend_Session::isStarted ()) {
					$sess = new Zend_Session_Namespace ( 'Kzend_Auth' );
					$sess->currentModule = $request->getModuleName ();
					$sess->currentController = $request->getControllerName ();
					$sess->currentAction = $request->getActionName ();
					$sess->currentRequestUri = $_SERVER ['REQUEST_URI'];
				}
				
				$frontController = Zend_Controller_Front::getInstance ();
				$response = $frontController->getResponse ();
				if (! empty ( $act )) {
					$response->setRedirect ( $frontController->getBaseUrl () . '/' . $con . '/' . $act );
				} else {
					$response->setRedirect ( $frontController->getBaseUrl () . '/' . $con );
				}
				$response->sendHeaders ();
				exit ();
			}
		} elseif ($this->_startsWith ( $request->getControllerName (), 'member' )) {
			$auth = Zend_Auth::getInstance ();
			$auth->setStorage ( new Zend_Auth_Storage_Session ( 'MEMBER_AUTH' ) );
			// var_dump(Zend_Auth::getInstance()->getStorage()->read());die;
			if (! $auth->hasIdentity ()) {
				$memberNoAuthDispatchFor = $configs ['member_noauth_dispatch_for'];
				list ( $mod, $con, $act ) = explode ( ".", $memberNoAuthDispatchFor );
				
				if (Zend_Session::isStarted ()) {
					$sess = new Zend_Session_Namespace ( 'Kzend_Auth' );
					$sess->currentModule = $request->getModuleName ();
					$sess->currentController = $request->getControllerName ();
					$sess->currentAction = $request->getActionName ();
					$sess->currentRequestUri = $_SERVER ['REQUEST_URI'];
				}
				
				$frontController = Zend_Controller_Front::getInstance ();
				$response = $frontController->getResponse ();
				if (! empty ( $act )) {
					$response->setRedirect ( $frontController->getBaseUrl () . '/' . $con . '/' . $act );
				} else {
					$response->setRedirect ( $frontController->getBaseUrl () . '/' . $con );
				}
				
				$response->sendHeaders ();
				exit ();
			}
		}
	}
}
